package com.example.cfm.ch01_4;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class BindService extends Service {

    private int count;
    private boolean quit;
    private MyBinder binder = new MyBinder();
    public class MyBinder extends Binder{
        public int getCount()
        {
            // 获取Service得运行状态：count
            return count;
        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        System.out.println("Service is Binder");
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("Service is Created");
        new Thread(){
            @Override
            public void run() {
                while (!quit)
                {
                    try {
                        Thread.sleep(1000);
                    }catch (InterruptedException e){

                    }
                    count++;
                }
            }
        }.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.quit = true;
        System.out.println("Service is Destoryed");
    }

    @Override
    public boolean onUnbind(Intent intent) {

        System.out.println("Service is UnBinded");
        return true;
    }
}
